/*
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-03-15 19:54:44
 * @LastEditTime: 2022-05-19 22:13:20
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description:
*/
import Dayjs from 'dayjs'
import rt from 'dayjs/plugin/relativeTime'
// 语言包
import 'dayjs/locale/zh-cn'
import store from '@/store'

export const formatDate = (val, format = 'YYYY-MM-DD') => {
  if (!isNaN(val)) {
    val = parseInt(val)
  }
  return Dayjs(val).format(format)
}

// 加载相对时间插件
Dayjs.extend(rt)
function relativeTime(val) {
  if (!isNaN(val)) {
    val = parseInt(val)
  }
  return Dayjs()
    .locale(store.getters.language === 'zh' ? 'zh-cn' : 'en')
    .to(Dayjs(val))
}
export default app => {
  app.config.globalProperties.$filters = {
    formatDate,
    relativeTime
  }
}
